//
//  AppDelegate.m
//  Double
//
//  Created by 袁庆秋 on 15/5/21.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "AppDelegate.h"
#import "CartoonMainViewController.h"
#import "SubjectViewController.h"
#import "ManHuaMainViewController.h"
#import "MessageViewController.h"
#import "MineViewController.h"
#import "RecommendViewController.h"
#import <AFNetworking/AFNetworking.h>
#import "WeiboSDK.h"





@interface AppDelegate ()<WeiboSDKDelegate>

@end

#define kAppKey         @"1837158850"
#define kRedirectURI    @"http://www.sharesdk.cn"

@implementation AppDelegate

- (void)dealloc
{
    [super dealloc];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
    [WeiboSDK enableDebugMode:YES];
    [WeiboSDK registerApp:@"1837158850"];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window setBackgroundColor:[UIColor whiteColor]];
    [self.window makeKeyAndVisible];
    
    
    UIImage *image = [UIImage imageNamed:@"00.jpg"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = [[UIScreen mainScreen] bounds];
    [self.window addSubview:imageView];
    [imageView release];
    
    [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        imageView.alpha = 0;
    } completion:^(BOOL finished) {
        
        CartoonMainViewController *subject = [[CartoonMainViewController alloc] init];
        subject.tabBarItem.title = @"动画";
        subject.tabBarItem.image = [UIImage imageNamed:@"film.png"];
        

        UINavigationController * flip = [[UINavigationController alloc]initWithRootViewController:subject];


        
        ManHuaMainViewController *dis = [[ManHuaMainViewController alloc] init];
        dis.tabBarItem.title = @"漫画";
        dis.tabBarItem.image = [UIImage imageNamed:@"dongman.png"];
        UINavigationController * flip1 = [[UINavigationController alloc]initWithRootViewController:dis];
        
        MessageViewController *message = [[MessageViewController alloc] init];
        message.tabBarItem.title = @"Coser";
        message.tabBarItem.image = [UIImage imageNamed:@"coser.png"];

        UINavigationController * flip2 = [[UINavigationController alloc]initWithRootViewController:message];
        
        MineViewController *mine = [[MineViewController alloc] init];
        mine.tabBarItem.title = @"我的";
        mine.tabBarItem.image = [UIImage imageNamed:@"wode.png"];

        UINavigationController * flip3 = [[UINavigationController alloc]initWithRootViewController:mine];
        
        UITabBarController *tab = [[UITabBarController alloc] init];
        tab.viewControllers = [NSArray arrayWithObjects:flip,flip1,flip2,flip3, nil];
        [self.window setRootViewController:tab];
        tab.tabBar.tintColor = [UIColor orangeColor];
        
        [subject release];
        [dis release];
        [message release];
        [mine release];
        [tab release];
    }];
    
    return YES;
}


- (void)didReceiveWeiboRequest:(WBBaseRequest *)request
{
    
}

- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{

    NSLog(@"userInfo = %@", response.userInfo);
    
    NSString *userId = [response.userInfo objectForKey:@"uid"];
    [[NSUserDefaults standardUserDefaults] setObject:userId forKey:@"loginName"];
    [[NSUserDefaults standardUserDefaults] setObject:@"登录" forKey:@"state"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    NSLog(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"loginName"]);
    
    // 网络请求获得用户信息
    [WBHttpRequest requestWithURL:[NSString stringWithFormat:@"https://api.weibo.com/2/users/show.json?access_token=%@&uid=%@", [response.userInfo objectForKey:@"access_token"], [response.userInfo objectForKey:@"uid"]] httpMethod:@"GET" params:nil queue:[NSOperationQueue mainQueue] withCompletionHandler:^(WBHttpRequest *httpRequest, id result, NSError *error) {
        
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"userInformation" object:nil userInfo:result];
        
    }];
    
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [WeiboSDK handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [WeiboSDK handleOpenURL:url delegate:self ];
}





- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
